var UBBCode = Class.create();
Object.extend(UBBCode, {
	output: function(s){
		document.write(this.transform(s));
	},
	transform: function(s){
		s = s.replace(/\n/g, '<br/>');
		return this.convert(s);
	},
	convert: function (c){
		var noimg = null;
		c = c.replace(/\r/g,'');

		c = c.replace(/\n/g,' ');

		c = c.replace(/\[s\:(\d+?)\]/gi, '<img src="http://i.178.com/images/smiles/em$1.gif"/>');//评论表情
		
		c = c.replace(/\[u\](.+?)\[\/u\]/gi,"<u>$1</u>");//[u]
		c = c.replace(/\[b\](.+?)\[\/b\]/gi,"<b>$1</b>");//[b]
		c = c.replace(/\[i\](.+?)\[\/i\]/gi,"<i style='font-style:italic'>$1</i>");//[i]

		c = c.replace(/(<br\s*\/?>)*\s*\[(\/?)list(=.+?)?\]\s*(<br\s*\/?>)*/gi,function ($0,$1,$2,$3){ if($3){return("<"+$3+"ul type"+$3+">")}else{return("<"+$2+"ul>")} } );//[list]
		c = c.replace(/\[\*\]/gim,"<li>");//[list]

		c = c.replace(/\[quote\](.+?)\[\/quote\]/gi,"<div class='quote'>$1</div>");//[quote]

		c = c.replace(/\[color=(skyblue|royalblue|blue|darkblue|orange|orangered|crimson|red|firebrick|darkred|green|limegreen|seagreen|teal|deeppink|tomato|coral|purple|indigo|burlywood|sandybrown|sienna|chocolate|silver|gray)\](.+?)\[\/color\]/gi,"<span class='$1'>$2</span>");//[color]

		c = c.replace(/\[email=(.{1,50}?)\](.+?)\[\/email\]/gi,"<a href='mailto:$1'>$2</a>");//[email\]

		c = c.replace(/\[email\](.+?)\[\/email\]/gi,"<a href='mailto:$1'>$1</a>");//[email\]

		c = c.replace(/\[size=(\d{1,3})%\](.+?)\[\/size\]/gi,function($0,$1,$2){return "<span style='font-size:"+$1+"%;line-height:"+$1+"%'>"+$2+"</span>"});//[size\]

		c = c.replace(/\[font=(simsun|simhei|Arial|Arial Black|Book Antiqua|Century Gothic|Comic Sans MS|Courier New|Georgia|Impact|Tahoma|Times New Roman|Trebuchet MS|Script MT Bold|Stencil|Verdana|Lucida Console)\](.+?)\[\/font\]/gi,"<span style='font-family:$1'>$2</span>");//[font]

		c = c.replace(/(\[align=)(left|center|right)(\])(.+?)(\[\/align\])/gi,"<div style='text-align:$2'>$4</div>");//[align]
/*
		if (!noimg || typeof(noimg)=='object')
			{
			c = c.replace(/\[img(\d{0,3})\](.+?)\[\/img\]/gi,function($0,$1,$2){
				if($2.match(/^[\x00-\x7F]+$/)) 
					var src=$2; 
				else
					var src='';
				if (src && src.substr(0,2).toLowerCase()=='./')
					src = __ATTACH_BASE+'/'+src.substr(2);
				if(src && src.substr(0,7).toLowerCase()!='http://') 
					src = 'http://'+src;
				if($1 && $1<=100 && $1>0) 
					$1="style='width:"+($1-0.1)+"%'";
				else
					$1='';
				var tmp ='';
				if (typeof(noimg)=='object')
					{
					if (noimg.h)
						tmp+= 'if(this.offsetHeight>'+noimg.h+')this.style.height="'+noimg.h+'px";';
					if (noimg.w)
						tmp+= 'if(this.offsetWidth>'+noimg.w+')this.style.height="'+noimg.w+'px";';
					}
				//if(src.match(/ngacn\.cc/gi))return "为了保证正常浏览，暂不显示签名中的图片";
				return("<img "+$1+" src='"+src+"' alt='' onload='"+tmp+"' onerror='this.nextSibling.style.display=\"inline\"'/><span class='silver' style='display:none'> [ "+$2+" ] </span>")
				});//[img]

			c = c.replace(/\[wmv\](.+?)\[\/wmv\]/gi,function($0,$1){ var u=$1;if(u.substr(0,7).toLowerCase()!='http://'){u = 'http://'+u} if (checklink(u,1)==1 && videonum<1) {videonum++;return("<object class='video' id='mplayer' classid='CLSID:22d6f312-b0f6-11d0-94ab-0080c74c7e95' codebase='http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=6,4,5,715' align='baseline' border='0' standby='Loading Microsoft Windows Media Player components...' type='application/x-oleobject'><param name='FileName' value='"+u+"'><param name='ShowControls' value='1'><param name='ShowPositionControls' value='0'><param name='ShowAudioControls' value='1'><param name='ShowTracker' value='1'><param name='ShowDisplay' value='0'><param name='ShowStatusBar' value='1'><param name='AutoSize' value='1'><param name='ShowGotoBar' value='0'><param name='ShowCaptioning' value='0'><param name='AutoStart' value='0'><param name='PlayCount' value='1'><param name='AnimationAtStart' value='0'><param name='TransparentAtStart' value='0'><param name='AllowScan' value='0'><param name='EnableContextMenu' value='1'><param name='ClickToPlay' value='0'><param name='InvokeURLs' value='1'><param name='DefaultFrame' value='datawindow'><embed src='"+u+"' align='baseline' border='0' type='application/x-mplayer2' pluginspage='http://www.microsoft.com/isapi/redir.dll?prd=windows&amp;sbp=mediaplayer&amp;ar=media&amp;sba=plugin&amp;' name='MediaPlayer' showcontrols='1' showpositioncontrols='0' showaudiocontrols='1' showtracker='1' showdisplay='0' showstatusbar='1' autosize='1' showgotobar='0' showcaptioning='0' autostart='0' autorewind='0' animationatstart='0' transparentatstart='0' allowscan='1' enablecontextmenu='1' clicktoplay='0' invokeurls='1' defaultframe='datawindow'></embed></object>");} else {return("<a href='"+u+"' target='_blank'>"+u+"</a>");} } );//[wmv]

			c = c.replace(/\[flash\](.+?)\[\/flash\]/gi,function($0,$1){ var u=$1;if(u.substr(0,7).toLowerCase()!='http://'){u = 'http://'+u} if (checklink(u,1)==1 && videonum<1) {videonum++;return("<object class='video' width='425px' height='350px' classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0'><param name='movie' value='"+u+"'><embed src='"+u+"' quality='high' type='application/x-shockwave-flash' width='425px' height='350px'></object>");} else {return("<a href='"+u+"' target='_blank'>"+u+"</a>");} } );//[flash]
			}
*/
		var self = this;

		c = c.replace(/\[url=(https?|ftp|gopher|news|telnet|mms|rtsp|)(.+?)\](.+?)\[\/url\]/gi,function($0,$1,$2,$3){if(!$1)$1='http://';return(self.writelink($1+$2,$3))});//[url]

		c = c.replace(/\[url\](bbs|www|db|ngacn)(\..+?)\[\/url\]/gi,function($0,$1,$2){return(self.writelink("http://"+$1+$2,"http://"+$1+$2))});//[url]

		c = c.replace(/\[url\](https?|ftp|gopher|news|telnet|mms|rtsp)(.+?)\[\/url\]/gi,function($0,$1,$2){return(self.writelink($1+$2,$1+$2))});//[url]

		c = c.replace(/\[img\]([^\[]+)\[\/img\]/gi, "<img src='$1'/>");//[url]
	

		c = c.replace(/\[(tid|pid)=?(\d{0,20})\](.+?)\[\/(tid|pid)\]/gi,function($0,$1,$2,$3,$4){
			if ($2)
				{return writelink('http://bbs.ngacn.cc/read.php?'+$1+'='+$2,$3)}
			else
				{
				if ($1=='tid')
					{
					var txt = '主题'
					}
				else
					{
					var txt = '回复'
					}
				return writelink('http://bbs.ngacn.cc/read.php?'+$1+'='+$3,txt+$3)}
			})

		c = c.replace(/\[s:(\d{1,11})\]/gi,function($0,$1){ return("<img src='"+bbscodesmiles[$1]+"' alt=''/>") } );//[smile]

		c = c.replace(/\[h\](.*?)\[\/h\]/gi,function($0,$1){if($1){return "<h4>"+$1+"</h4>"}else{return "<h4 style='line-height:0;font-size:0;padding:0;margin:0 0 3px 0;height:0'></h4>"}});//[h]
		c = c.replace(/={3,100}(.+?)={3,100}/gi,function($0,$1){if($1=='=')$1=''; return "<h4>"+$1+"</h4>"});//[h]
		c = c.replace(/\s*(<br\s*\/?>)+\s*<(\/?)h4>\s*(<br\s*\/?>)+\s*/gi,"<$1h4>");//[h]


		c = c.replace(/(<br\s*\/?>)*\s*\[l(\d*)\]\s*(<br\s*\/?>)*\s*(.+?)\s*(<br\s*\/?>)*\s*\[\/l\]\s*(<br\s*\/?>)*/gi,function($0,$1,$2,$3,$4){if($2<=100){$2="style='width:"+($2-0.1)+"%'"}else{$2=''}return "<div "+$2+" class='left'>"+$4+"</div>"});//[left]
		c = c.replace(/(<br\s*\/?>)*\s*\[r(\d*)\]\s*(<br\s*\/?>)*\s*(.+?)\s*(<br\s*\/?>)*\s*\[\/r\]\s*(<br\s*\/?>)*/gi,function($0,$1,$2,$3,$4){if($2<=100){$2="style='width:"+($2-0.1)+"%'"}else{$2=''}return "<div "+$2+" class='right'>"+$4+"</div>"});//[right]

		c = c.replace(/p_w_upload/gi,"attachment");//[img]

		return c;
	},
	writelink: function(url, alt){
		if(!alt) alt = url;
		return '<a href="'+url+'" target="_blank">'+alt+'</a>';
	}
});